#include "ContraData.h"




void ContraData::initSprite()
{
	// add all state of contra to sprite container
	sprites->addSprite(_STATE_CONTRA_STAND_RIGHT, _ID_CONTRA_LEG_STAND_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_MOVE_RIGHT, _ID_CONTRA_LEG_MOVE_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_STAND_LEFT, _ID_CONTRA_LEG_STAND_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_MOVE_LEFT, _ID_CONTRA_LEG_MOVE_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_SIT_LEFT, _ID_CONTRA_LEG_SIT_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_FIRE_SIT_LEFT, _ID_CONTRA_LEG_FIRE_SIT_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_FIRE_SIT_RIGHT, _ID_CONTRA_LEG_FIRE_SIT_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_SIT_RIGHT, _ID_CONTRA_LEG_SIT_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	sprites->addSprite(_STATE_CONTRA_FLY_RIGHT, _ID_CONTRA_LEG_FLY_RIGHT, _FRAMERATE_CONTRA_DEFAULT / 2);
	sprites->addSprite(_STATE_CONTRA_FLY_LEFT, _ID_CONTRA_LEG_FLY_LEFT, _FRAMERATE_CONTRA_DEFAULT / 2);
	sprites->addSprite(_STATE_CONTRA_DEAD, _ID_CONTRA_DEAD, _FRAMERATE_CONTRA_DEFAULT);
	//sprites->addSprite(_STATE_CONTRA_SIT_FIRE_RIGHT, _ID_CONTRA_SIT_FIRE_RIGHT);
	// add sprite to body sprites
	bodySprites->addSprite(_STATE_CONTRA_MOVE_LEFT, _ID_CONTRA_BODY_MOVE_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_LEFT, _ID_CONTRA_BODY_FIRE_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_UP_LEFT, _ID_CONTRA_BODY_FIRE_UP_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_UP_LEFT, _ID_CONTRA_BODY_UP_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_UP_CROSS_LEFT, _ID_CONTRA_BODY_UP_CROSS_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_UP_CROSS_LEFT, _ID_CONTRA_BODY_FIRE_UP_CROSS_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_DOWN_CROSS_LEFT, _ID_CONTRA_BODY_DOWN_CROSS_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_DOWN_CROSS_LEFT, _ID_CONTRA_BODY_FIRE_DOWN_CROSS_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_STAND_LEFT, _ID_CONTRA_BODY_STAND_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_MOVE_RIGHT, _ID_CONTRA_BODY_MOVE_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_RIGHT, _ID_CONTRA_BODY_FIRE_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_UP_RIGHT, _ID_CONTRA_BODY_FIRE_UP_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_UP_RIGHT, _ID_CONTRA_BODY_UP_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_UP_CROSS_RIGHT, _ID_CONTRA_BODY_UP_CROSS_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_UP_CROSS_RIGHT, _ID_CONTRA_BODY_FIRE_UP_CROSS_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_DOWN_CROSS_RIGHT, _ID_CONTRA_BODY_DOWN_CROSS_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_DOWN_CROSS_RIGHT, _ID_CONTRA_BODY_FIRE_DOWN_CROSS_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_STAND_RIGHT, _ID_CONTRA_BODY_STAND_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SIT_LEFT, _ID_CONTRA_BODY_SIT_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_SIT_LEFT, _ID_CONTRA_BODY_FIRE_SIT_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FIRE_SIT_RIGHT, _ID_CONTRA_BODY_FIRE_SIT_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SIT_RIGHT, _ID_CONTRA_BODY_SIT_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_FLY_RIGHT, _ID_CONTRA_BODY_FLY_RIGHT, _FRAMERATE_CONTRA_DEFAULT / 2);
	bodySprites->addSprite(_STATE_CONTRA_FLY_LEFT, _ID_CONTRA_BODY_FLY_LEFT, _FRAMERATE_CONTRA_DEFAULT / 2);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_LEFT, _ID_CONTRA_BODY_SWIM_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_RIGHT, _ID_CONTRA_BODY_SWIM_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_LEFT_FIRE, _ID_CONTRA_BODY_SWIM_LEFT_FIRE, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_RIGHT_FIRE, _ID_CONTRA_BODY_SWIM_RIGHT_FIRE, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_DOWN, _ID_CONTRA_BODY_SWIM_DOWN, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_FIRE_UP_LEFT, _ID_CONTRA_BODY_SWIM_FIRE_UP_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_FIRE_UP_RIGHT, _ID_CONTRA_BODY_SWIM_FIRE_UP_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_FIRE_CROSS_LEFT, _ID_CONTRA_BODY_SWIM_FIRE_CROSS_LEFT, _FRAMERATE_CONTRA_DEFAULT);
	bodySprites->addSprite(_STATE_CONTRA_SWIM_FIRE_CROSS_RIGHT, _ID_CONTRA_BODY_SWIM_FIRE_CROSS_RIGHT, _FRAMERATE_CONTRA_DEFAULT);
	// Set default state for contra
	sprites->setState(_STATE_CONTRA_STAND_RIGHT);
	bodySprites->setState(_STATE_CONTRA_STAND_RIGHT);
}

ContraData::ContraData(int id, double x, double y)
{
	this->id = id;
	this->type = _ID_CONTRA;
	this->onJump = false;
	// Init contra rect bound
	this->rect = new Rect(x, y, _DATA_CONTRA_HEIGHT, _DATA_CONTRA_WIDTH);
	this->rect->Vx = this->rect->Vy = 0;
	// init sprite containser
	sprites = new Sprites(this->rect);
	bodySprites = new Sprites(this->rect);
	// init bullet container
	bulletContainer = new BulletContainer(this->rect, BulletPlayer);
	bulletContainer->updateData(_DATA_CONTRA_BULLET_1_DISTANCE_LIVE, _DATA_CONTRA_BULLET_1_DELAY, _DATA_CONTRA_BULLET_1_MAX, _ID_BULLET_LEVEL_1);
	timeDelayShooting = _DATA_CONTRA_TIME_DELAY_SHOOTING;
	jumpingTimeCounter = 0;
	readyToFly = true;
	isSwimming = false;
	updateTime = changeStateTime = 0;
	undeadDelay = _DATA_CONTRA_UNDEAD;
	notMove = false;
}

void ContraData::initData()
{
	this->type = _TYPE_CONTRA;
	// Init all sprites data
	initSprite();
}

Sprites* ContraData::getSprites()
{
	return sprites;
}

void ContraData::initSpriteHandler(LPD3DXSPRITE spriteHandler)
{
	sprites->initSpriteHandler(spriteHandler);
	bodySprites->initSpriteHandler(spriteHandler);
}

bool ContraData::checkIsMoving()
{
	return getRect()->Vx != 0;
}

bool ContraData::checkCorrectState()
{
	return bodySprites->currentSprite->currentIndex() == sprites->currentSprite->currentIndex();
}

bool ContraData::checkDelayShooting()
{
	//// check timeDelayShooting 
	//if (timeDelayShooting < 0 && bulletContainer->checkCanFire()) {
	//	// reset time
	//	return false;
	//}
	//else {
	//	timeDelayShooting--;
	//	return true;// && 
	//}
	return !bulletContainer->checkCanFire();
}

void ContraData::resetTimeDelayShooting()
{
	timeDelayShooting = _DATA_CONTRA_TIME_DELAY_SHOOTING;
}

bool ContraData::checkIsFlying()
{
	//(sprites->state == _STATE_CONTRA_FLY_LEFT) || (sprites->state == _STATE_CONTRA_FLY_RIGHT) || 
	return (jumpingTimeCounter > 0 || !readyToFly || onJump);
}

void ContraData::updateData(ACTIONS action, int value)
{
	switch(action) {
	case ACTIONS_STOP_FLYING:
		jumpingTimeCounter = 0;
		onJump = false;
		readyToFly = true;
	}
}
